<?php
defined('IN_PHPCMS') or exit('No permission resources.');
//保存图片
function GrabImage($url, $img_path){
	if ($url == ""){
		return false;
	}
	$ext = strrchr($url, ".");
	$filename = date("Ymdhis").rand(100, 999).$ext;			
 	ob_start();//打开输出
 	readfile($url);//输出图片文件
 	$img = ob_get_contents();//得到浏览器输出
 	ob_end_clean();//清除输出并关闭
 	$size = strlen($img);//得到图片大小
 	$fp2 = @fopen($img_path.$filename, "a");
 	fwrite($fp2, $img);//向当前目录写入图片文件，并重新命名
 	fclose($fp2);
 	$picpath = $img_path.$filename;
 	$oss_status = oss_upload($picpath, $picpath);
 	if(OSS_STATUS == 1){
 		if($oss_status['info']['http_code'] == 200){
 			return OSS_PATH.$picpath;
 		}else{
 			return false;
 		} 		
 	}else{
 		return APP_PATH.$picpath;
 	}
}
//生成目录
function MakeDir(){
	$img_path = "uploadfile/".date("Y/md/");
	if(!file_exists($img_path)){
		mkdir($img_path, 0777, ture);
	}
	return $img_path;
}
//获取页面内容
$picurl = $_GET['url'];
$picsite = $_GET['site'];
$item = file_get_contents($picurl);

function uumnt($picurl, $item){
	preg_match_all("/<h1 class=\"center\">(.*?)\(1\/(.*?)\)<\/h1>/i", $item, $b);
	if($b){
		$data[0] = 1;
		$data[1]['title'] = trim($b[1][0]);
		$piclistnum = $b[2][0];
		//$pic = array();
		for ($i=1; $i<=$piclistnum; $i++) { 
			if($i == 1){
				$pic[$i] = $picurl;
			}else{
				$pic[$i] = str_replace(".html", "_".$i.".html", $picurl);
			}
		}
		//循环存储图片
		foreach ($pic as $key => $value) {
			$a = file_get_contents($value);
			preg_match_all("/<img src=\"(.*?)\" alt=\"(.*?)\" \/>/i", $a, $b);
			$picsavename = GrabImage($b[1][0], MakeDir());
			if($picsavename){
				$picname[$key] = $picsavename;
			}else{
				continue;
			}	
		}
		$data[1]['list'] = $picname;	
	}else{
		$data[0] = 0;
	}
	echo json_encode($data);
}

function t27270($picurl, $item){
	$item = iconv('gbk', 'utf-8', $item);
	preg_match_all("/<h1 class=\"articleV4Tit\">(.*?)<\/h1>/i", $item, $b);
	if($b){
		$data[0] = 1;
		$data[1]['title'] = trim($b[1][0]);
		preg_match_all("/<a>共(.*?)页: <\/a>/i", $item, $page);
		$piclistnum = $page[1][0];
		for ($i=1; $i<=$piclistnum; $i++) { 
			if($i == 1){
				$pic[$i] = $picurl;
			}else{
				$pic[$i] = str_replace(".htm", "_".$i.".htm", $picurl);
			}
		}
		foreach ($pic as $key => $value) {
			$a = file_get_contents($value);
			$a = iconv("gbk", "utf-8", $a);
			preg_match_all("/<a href='(.*?)' ><img alt=\"(.*?)\"  src=\"(.*?)\" \/><\/a>/i", $a, $b);
			$picsavename = GrabImage($b[3][0], MakeDir());
			if($picsavename){
				$picname[$key] = $picsavename;
			}else{
				continue;
			}
		}
		$data[1]['list'] = $picname;
	}else{
		$data[0] = 0;
	}
	echo json_encode($data);
}

function duowan($picurl, $item){
	preg_match_all("/<a href=\"(.*?)\" class=\"picture-list\">/i", $item, $scrollpage);
	$picurl = $scrollpage[1][0];
	$item = file_get_contents($picurl);
	preg_match_all("/<h1>(.*?)<\/h1>/i", $item, $b);
	if($b){
		$data[0] = 1;
		$data[1]['title'] = trim($b[1][0]);
		preg_match_all("/<span class=\"pic-box-item\" data-img=\"(.*?)\" data-mp4=\"\"><\/span>/i", $item, $b);
		$pic = $b[1];
		//循环存储图片
		foreach ($pic as $key => $value) {
			$picsavename = GrabImage($value, MakeDir());
			if($picsavename){
				$picname[$key] = $picsavename;
			}else{
				continue;
			}

		}
		$data[1]['list'] = $picname;
	}else{
		$data[0] = 0;
	}
	echo json_encode($data);
}

function mmonly($picurl, $item){
	$item = iconv('gbk', 'utf-8', $item);
	preg_match_all("/<h1>(.*?)<span id=\"picnum\">\(<span class=\"nowpage\">1<\/span>\/<span class=\"totalpage\">(.*?)<\/span>\)<\/span><\/h1>/i", $item, $b);
	if($b){
		$data[0] = 1;
		$data[1]['title'] = trim($b[1][0]);
		$piclistnum = $b[2][0];
		for ($i=1; $i<=$piclistnum; $i++) { 
			if($i == 1){
				$pic[$i] = $picurl;
			}else{
				$pic[$i] = str_replace(".html", "_".$i.".html", $picurl);
			}
		}
		//循环存储图片
		foreach ($pic as $key => $value) {
			$a = file_get_contents($value);
			$a = iconv('gbk', 'utf-8', $a);
			preg_match_all("/<a href='(.*?)'><img alt=\"(.*?)\" src=\"(.*?)\" \/><\/a>/i", $a, $b);
			$picsavename = GrabImage($b[3][0], MakeDir());
			if($picsavename){
				$picname[$key] = $picsavename;
			}else{
				continue;
			}	
		}
		$data[1]['list'] = $picname;
	}else{
		$data[0] = 0;
	}
	echo json_encode($data);
}

function doyo($picurl, $item){
	preg_match_all("/<h1>(.*?)<\/h1>/i", $item, $b);
	if($b){
		$data[0] = 1;
		$data[1]['title'] = trim($b[1][0]);
		preg_match_all("/<span class=\"place\">第 <strong>1<\/strong> \/ (.*?) 张<\/span>/i", $item, $page);
		$piclistnum = $page[1][0];
		for ($i=1; $i<=$piclistnum; $i++) { 
			if($i == 1){
				$pic[$i] = $picurl;
			}else{
				$pic[$i] = $picurl."/".$i;
			}
		}
		foreach ($pic as $key => $value) {
			$a = file_get_contents($value);
			preg_match_all("/<div class=\"tools\"><a href=\"(.*?)\" target=\"_blank\">/i", $a, $c);
			$url = "http://www.doyo.cn".$c[1][0];
			$item = file_get_contents($url);
			preg_match_all("/<img src=\"(.*?)\" id=\"big_picture\" \/>/i", $item, $b);
			$picsavename = GrabImage($b[1][0], MakeDir());
			if($picsavename){
				$picname[$key] = $picsavename;
			}else{
				continue;
			}	
		}
		$data[1]['list'] = $picname;
	}else{
		$data[0] = 0;
	}
	echo json_encode($data);
}

switch ($picsite) {
	case 'uumnt':
		uumnt($picurl, $item);
		break;
	case 't27270':
		t27270($picurl, $item);
		break;
	case 'duowan':
		duowan($picurl, $item);
		break;
	case 'mmonly':
		mmonly($picurl, $item);
		break;
	case 'doyo':
		doyo($picurl, $item);
		break;	
	default:
		$data[0] = 0;
		echo json_encode($data);
		break;
}
?>